/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2022-12-05 10:29:58
 * @LastEditors: TT
 * @LastEditTime: 2022-12-27 17:46:30
 */

import 'dart:typed_data';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:hzy_normal_widget/hzy_normal_widget.dart';
import 'common_extendeed_image_widget.dart';
import 'common_long_graph_widget.dart';
import 'common_state_widget.dart';
import 'package:extended_image_library/extended_image_library.dart';

class CommonBannerPage extends StatefulWidget {
  /// 图片数组
  final List<File>? list;

  final List<String>? imageUrlList;
  final List<ImageNormalModel>? imageModellist;

  /// 设置默认 位置
  final int? initialPage;

  final bool? iskeep;

  final bool? cache;

  final String? cahchename;

  /// 轮播图
  final Function(int index)? onPageChanged;
  final Color? backgroudColor;
  final List<Uint8List>? bytelist;

  final bool isgesture;

  final String? heroStr;
  const CommonBannerPage({
    Key? key,
    this.list,
    this.initialPage,
    this.iskeep,
    this.onPageChanged,
    this.backgroudColor,
    this.bytelist,
    this.heroStr,
    this.cache = true,
    this.imageUrlList,
    this.cahchename,
    this.isgesture = true,
    this.imageModellist,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return CommonBannerPageState();
  }
}

class CommonBannerPageState extends CommonStateWidget<CommonBannerPage>
    with TickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  /// 属性
  ///
  ///
  late AnimationController doubleClickAnimationController;
  @override
  bool get safeAreabottm => false;
  @override
  bool get safeAreatop => false;
  @override
  Color? get backgroundColor =>
      widget.backgroudColor ?? CommentColorS.col000000;
  int allnum = 0;
  late ExtendedPageController extendedPageController;
  late AnimationController _controller;

  int normalindex = 0;

  /// 生命周期
  ///
  ///

  /// 界面初始化
  @override
  void initDefaultState() {
    allnum = (widget.list?.length ?? widget.bytelist?.length) ?? 0;
    extendedPageController =
        ExtendedPageController(initialPage: widget.initialPage ?? 0);
    doubleClickAnimationController = AnimationController(
      duration: const Duration(milliseconds: 150),
      vsync: this,
    );
    normalindex = widget.initialPage ?? 0;
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 200),
      lowerBound: 0.0,
      upperBound: 1.0,
    );
    super.initDefaultState();
  }

  /// 界面销毁
  @override
  void initDefaultDispose() {
    super.initDefaultDispose();
    _controller.dispose();
    extendedPageController.dispose();
    doubleClickAnimationController.dispose();
  }

  /// 网路请求
  ///
  ///

  /// 构建视图
  ///
  ///

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return createScaffol(
      context: context,
      safeAreatop: safeAreatop,
      safeAreabottm: safeAreabottm,
      pageState: pageState,
      title: title,
      backgroundColor: backgroundColor,
      navbackcolor: navbackcolor,
      resizeToAvoidBottomInset: resizeToAvoidBottomInset,
    );
  }

  @override
  Widget createBody(BuildContext context) {
    return createBannerWidget();
  }

  Widget createBannerWidget() {
    int allnum = widget.bytelist?.length ??
        (widget.list?.length ??
            (widget.imageUrlList?.length ??
                widget.imageModellist?.length ??
                0));
    return ExtendedImageGesturePageView.builder(
      itemBuilder: (context, index) {
        String? imageurl;
        double? w;
        double? h;
        if (widget.imageModellist?[index] != null) {
          ImageNormalModel model = widget.imageModellist![index];
          imageurl = model.imageeUrl;
          w = model.imageSize?.width;
          h = model.imageSize?.height;
        }
        return ExtendedImageWidget.createbannerimage(
          imagefile: widget.list?[index],
          bytes: widget.bytelist?[index],
          imageurl: imageurl,
          isgesture: widget.isgesture,
          width: w,
          height: h,
          animationController: _controller,
          herostr: widget.heroStr == null ? null : "${widget.heroStr}$index",
          doubleClickAnimationController: doubleClickAnimationController,
          imageCacheName: widget.cahchename,
        );
      },
      controller: extendedPageController,
      scrollDirection: Axis.horizontal,
      physics: const BouncingScrollPhysics(),
      itemCount: allnum,
      onPageChanged: (int index) {
        if (widget.onPageChanged != null && normalindex != index) {
          widget.onPageChanged!(index);
        }
      },
    );
  }

  /// 触发方法
  ///
  ///

  /// 配置默认数据

  @override
  bool get wantKeepAlive => widget.iskeep ?? true;

  void reloadState() {
    configsetState(() {});
  }

  void changebannerindex(int index) {
    if (normalindex != index) {
      extendedPageController.jumpToPage(index);
    }
  }
}
